<template>
  <div class="goods-size-content">
    <div v-if="isEdited">
      <span>
        <template v-if="showAttrType === 1">
          {{goodsRow.goodsNumInBox}}
        </template>
        <template v-else-if="showAttrType === 2">
          {{goodsRow.weight}}kg ({{goodsRow.length}}×{{goodsRow.width}}×{{goodsRow.height}}cm)
        </template>
        <template v-else-if="showAttrType === 3">
          {{goodsRow.goodsNumInBox}} - {{goodsRow.weight}}kg ({{goodsRow.length}}×{{goodsRow.width}}×{{goodsRow.height}}cm)
        </template>
      </span>
      <span>
         <i class="el-icon-edit mgl10" style="cursor: pointer" @click="showEditDialog" v-if="canEdit"></i>
      </span>
    </div>
    <div v-else>
        <el-button type="text" @click="showEditDialog" :loading="loading">编辑箱规</el-button>
        <i class="el-icon-edit mgl10" style="cursor: pointer" @click="showEditDialog"></i>
    </div>


    <el-dialog
      :visible.sync="dialog"
      width="600px"
      class="goods-size-dialog"
      title="编辑箱规"
    >
      <div class="goods-size-dialog-content">
        <div class="goods-size-dialog-sub-title" v-if="goodsTitle">
          {{ $t('table.title') }}: {{goodsTitle}}
        </div>
        <div class="goods-size-tips font-red">
          * 填写当前箱规装满箱装数商品后的重量与尺寸
        </div>
        <div class="goods-size-dialog-form">
          <el-form
            ref="sizeForm"
            :model="currentForm"
            label-width="80px"
            label-position="left"
            :rules="rules"
          >

            <el-form-item
              label="箱装数"
              prop="goodsNumInBox"
            >
              <el-input-number
                v-model="currentForm.goodsNumInBox"
                :controls="false"
                :min="1"
                :max="max"
                :precision="0"
                class="weight-input"
              >
              </el-input-number>
            </el-form-item>

            <el-form-item
              :label="$t('table.重量')"
              prop="weight"
            >
              <el-input-number
                v-model="currentForm.weight"
                :controls="false"
                :min="0"
                :max="max"
                :precision="3"
                class="weight-input"
              >
              </el-input-number>
              <span class="mgl10">kg</span>

            </el-form-item>
            <el-form-item
              :label="$t('table.尺寸')"
              required
            >
              <div class="size-input-box">
                <el-form-item prop="length">
                  <el-input-number
                    v-model="currentForm.length"
                    class="size-input"
                    :min="0"
                    :max="max"
                    :precision="0"
                    :controls="false"
                  >
                  </el-input-number>
                  <span class="mgl10">{{ $t('stock.长(cm)') }}</span>
                </el-form-item>

                <el-form-item prop="width">
                  <el-input-number
                    v-model="currentForm.width"
                    class="size-input"
                    :min="0"
                    :max="max"
                    :precision="0"
                    :controls="false"
                  >
                  </el-input-number>
                  <span class="mgl10">{{ $t('stock.宽(cm)') }}</span>
                </el-form-item>

                <el-form-item prop="height">
                  <el-input-number
                    v-model="currentForm.height"
                    class="size-input"
                    :min="0"
                    :max="max"
                    :precision="0"
                    :controls="false"
                  >
                  </el-input-number>
                  <span class="mgl10">{{ $t('stock.高(cm)') }}</span>
                </el-form-item>

              </div>

            </el-form-item>
            <el-form-item>
              <el-button size="small" type="primary" @click="submitGoodsSize()" :loading="loading">{{$t('common.save')}}</el-button>
              <el-button size="small" @click="dialog = false">{{$t('common.cancel')}}</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import {saveGoodsSize, saveGoodsSizeV2} from "@/api/goods";
import tree from "element-ui/packages/tree";
import request from "@/utils/request";

export default {
  name: "GoodsBoxSize",
  mixins: [],
  props:{
    goodsRow: {
      type: Object,
      required: true,
    },
    goodsTitle: {
      type: String,
      default: '',
    },
    suitId: {
      type: Number,
      required: true,
    },
    canEdit: {
      type: Boolean,
      default: true,
    },
    // 展示属性类型 1：只显示箱装数 2：显示重量尺寸 3：箱装数+重量尺寸
    showAttrType: {
      type: Number,
      default: 1,
    },
  },
  computed:{
    // 判断是否已经编辑过
    isEdited(){
      return !!this.goodsRow.weight && !!this.goodsRow.goodsNumInBox
    },
  },
  data(){
    return {
      max: 999999, // 设置一个最大值，避免用户乱填 导致接口异常
      currentForm: {
        id: 0,
        suitId: 0,
        weight: 0,
        length: 0,
        width: 0,
        height: 0,
        goodsNumInBox: 0,
      },
      rules: {
        weight: [
          {type:'number', required: true, message: this.$t('stock.重量请填写大于0的数字'), trigger: 'blur',min:0.1},
        ],
        length: [
          {type:'number', required: true, message: this.$t('stock.长请填写大于0的数字'), trigger: 'blur',min:1}
        ],
        width: [
          {type:'number', required: true, message: this.$t('stock.宽请填写大于0的数字'), trigger: 'blur',min:1},
        ],
        height: [
          {type:'number', required: true, message: this.$t('stock.高请填写大于0的数字'), trigger: 'blur',min:1},
        ],
        goodsNumInBox: [
          {type:'number', required: true, message: '箱装数请填写大于0的数字', trigger: 'blur',min:1},
        ]
      },
      dialog: false,
      loading: false,
    }
  },
  methods: {
    showEditDialog(){
      this.currentForm.id = this.goodsRow.id
      this.currentForm.weight = this.goodsRow.weight
      this.currentForm.length = this.goodsRow.length
      this.currentForm.width = this.goodsRow.width
      this.currentForm.height = this.goodsRow.height
      this.currentForm.goodsNumInBox = this.goodsRow.goodsNumInBox
      this.dialog = true
    },
    // 提交表单数据
    submitGoodsSize(){
      this.$refs.sizeForm.validate(valid => {
        if(valid){
          this.saveGoodsSize()
        }
      })
    },
    saveGoodsSize(){
      this.loading = true
      this.currentForm.suitId = this.suitId
      request({
        url: '/goods/saveGoodsBoxSize',
        method: 'put',
        handleError: true,
        data: this.currentForm,
      })
        .then(res=>{
          this.loading = false
          this.dialog = false
          this.goodsRow.weight = this.currentForm.weight
          this.goodsRow.length = this.currentForm.length
          this.goodsRow.width = this.currentForm.width
          this.goodsRow.height = this.currentForm.height
          this.goodsRow.goodsNumInBox = this.currentForm.goodsNumInBox
          this.$successMessage(res.message)
          this.$emit('saveGoodsBox', this.currentForm)
        })
    },
  }
}

</script>

<style scoped lang="scss">
.goods-size-dialog{
  text-align: left;
  .goods-size-dialog-content{
    margin-top: -25px;
    .goods-size-dialog-sub-title{
      font-size: 14px;
    }
    .goods-size-tips{
      font-size: 14px;
      margin: 3px 0 10px;
    }

    .goods-size-dialog-form{
      .weight-input{
        width: 100px;
      }
      .size-input-box{
        display: flex;
        flex-direction: row;
        justify-content: space-between;
        .size-input{
          width: 100px;
          ::v-deep .el-input-group__append{
            padding: 0 15px !important;
          }
        }
      }

    }
  }
}
</style>
